<?php
/**
 *  * 系统-受国家计算机软件著作权保护 - !
 * =========================================================
 * Copy right 2018-2025 成都海之心科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: http://www.ohyu.cn
 * 这不是一个自由软件！在未得到官方有效许可的前提下禁止对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 * User: ohyueo
 * Date: 2021/5/22
 * Time: 17:56
 */
namespace app\admin\controller;
use think\facade\View;
use think\facade\Db;
use app\admin\traits\AdminAuth;
use app\handler\WorkHandler;
use app\admin\models\EscortCallCalllist;
use app\admin\models\EscortCallOrder;

class Call extends Common
{
    use AdminAuth;

    public function calllist(){
 		if(request()->isPost()){
			$page=input('param.page');
			if(!$page){
				$page=1;
			}
			$limit=input('param.limit');
			if(!$limit){
				$limit=10;//每页显示条数
			}
			$where=[];
			$id=input('param.id');
			if($id){
				$where[]=['id','=',$id];
			}
			$merchant_bankid = session('merchant_bankid');
			if($merchant_bankid){
				$where[]=['id','=',$merchant_bankid];
			}
			//$count = Db::name('escort_call_calllist')->where($where)->count();
			$count = EscortCallCalllist::countData(['where'=>$where]);
			//$data=Db::name('escort_call_calllist')->where($where)->page($page,$limit)->order('id desc')->select()->toArray();
			$data = EscortCallCalllist::findAllData(['where'=>$where,'page'=>$page,'limit'=>$limit,'order'=>'id desc','isarr'=>true]);
			if($data){
			    for ($i=0;$i<count($data);$i++){
					//给pc加上网址
					$pc=$data[$i]['pc'];
					if($pc){
						$pc=config('-appsite.app_domainname').$pc;
					}
					$data[$i]['pcx']=$pc;
					//给anzhuo加上网址
					$anzhuo=$data[$i]['anzhuo'];
					if($anzhuo){
						$anzhuo=config('-appsite.app_domainname').$anzhuo;
					}
					$data[$i]['anzhuox']=$anzhuo;
				}
			}
			$res = ["data" => $data,"code"=>0,'message'=>'请求成功','count'=>$count];
			return json($res);
		}
		$permis = $this->getPermissions('Call/calllist');
		View::assign('data', $this->actions);
		View::assign('permis', $permis);
		return View::fetch();
	}
	public function addcalllist(){
 		if(request()->isPost()){
			$data = array('status' => 0, 'msg' => '未知错误');
			$title=input('param.title');
			$status=input('param.status');
			$paiid=input('param.paiid');
			$hechengtype=input('param.hechengtype');
			$pc=input('param.pc');
			$anzhuo=input('param.anzhuo');
			$arr=array(
				'title'=>$title,
				'status'=>$status,
				'paiid'=>$paiid,
				'hechengtype'=>$hechengtype,
				'pc'=>$pc,
				'anzhuo'=>$anzhuo,
			);
			//$id=Db::name('escort_call_calllist')->insertGetId($arr);
			$res=EscortCallCalllist::addData($arr);
			$id=$res->id;
			$text = '添加了机构列表-id='.$id;
			$this->writeActionLog($text);
			$data['status'] = 1;
			return json($data);exit;
		}
		return View::fetch('editcalllist');
	}
	public function editcalllist(){
 		if(request()->isPost()){
			$data = array('status' => 0, 'msg' => '未知错误');
			$title=input('param.title');
			$status=input('param.status');
			$paiid=input('param.paiid');
			$hechengtype=input('param.hechengtype');
			$pc=input('param.pc');
			$anzhuo=input('param.anzhuo');
			$arr=array(
				'title'=>$title,
				'status'=>$status,
				'paiid'=>$paiid,
				'hechengtype'=>$hechengtype,
				'pc'=>$pc,
				'anzhuo'=>$anzhuo,
			);
			$id = input('param.id');
			if ($id){
				//$id=Db::name('escort_call_calllist')->where('id',$id)->update($arr);
				EscortCallCalllist::updatesData(['where'=>['id'=>$id],'data'=>$arr]);
				$text = '修改了机构列表-id='.$id;
				$this->writeActionLog($text);
				$data['status'] = 1;
				return json($data);exit;
			}
		}
		return View::fetch('editcalllist');
	}
	public function delcalllist(){
 		$data = array('status' => 0,'msg' => '未知错误');
		$array=input("param.id");
		if(!$array){
			$data['msg']='参数错误';return json($data);exit;
		}
		$arr=explode(",",$array);
		for($i=0;$i<count($arr);$i++){
			if($arr[$i]){
				//Db::name('escort_call_calllist')->where('id',$arr[$i])->delete();
				EscortCallCalllist::deleteData(['where'=>['id'=>$arr[$i]]]);
				$text = '删除了机构列表id='.$arr[$i].'的数据';
				$this->writeActionLog($text);
			}
		}
		$data['status']=1;
		return json($data);
	}
	//叫号订单
	public function orderlist(){
		if(request()->isPost()){
		   $page=input('param.page');
		   if(!$page){
			   $page=1;
		   }
		   $limit=input('param.limit');
		   if(!$limit){
			   $limit=10;//每页显示条数
		   }
		   $where=[];
		   $id=input('param.id');
		   if($id){
			   $where[]=['id','=',$id];
		   }
		   //$count = Db::name('escort_call_order')->where($where)->count();
		   $count = EscortCallOrder::countData(['where'=>$where]);
		   //$data=Db::name('escort_call_order')->where($where)->page($page,$limit)->order('id desc')->select()->toArray();
		   $data = EscortCallOrder::findAllData(['where'=>$where,'page'=>$page,'limit'=>$limit,'order'=>'id desc','isarr'=>true]);
		   if($data){
			   //循环data
			   for($i=0;$i<count($data);$i++){
				   //$listid=$data[$i]['listid'];
				   //$data[$i]['listid']=Db::name('yuyue_list')->where('id',$listid)->value('title');
				   //$uid=$data[$i]['uid'];
				   //$data[$i]['nick']=Db::name('user_list')->where('id',$uid)->value('nick');
				   $bankid=$data[$i]['bankid'];
				   //$data[$i]['tit1']=Db::name('escort_call_calllist')->where('id',$bankid)->value('title');
				   $data[$i]['tit1']=EscortCallCalllist::valueData(['where'=>['id'=>$bankid],'field'=>'title']);
			   }
		   }
		   $res = ["data" => $data,"code"=>0,'message'=>'请求成功','count'=>$count];
		   return json($res);
	   }
	   $permis = $this->getPermissions('Call/orderlist');
	   View::assign('data', $this->actions);
	   View::assign('permis', $permis);
	   return View::fetch();
   }
   public function delorder(){
		$data = array('status' => 0,'msg' => '未知错误');
		$array=input("param.id");
		if(!$array){
			$data['msg']='参数错误';return json($data);exit;
		}
		$where=[];
		$merchant_bankid = session('merchant_bankid');
		if($merchant_bankid){
			$where[]=['bankid','=',$merchant_bankid];
		}
		$arr=explode(",",$array);
		for($i=0;$i<count($arr);$i++){
			//删除数据
			if($arr[$i]){
				$where[]=['id','=',$arr[$i]];
				//Db::name('escort_call_order')->where('id',$arr[$i])->where($where)->delete();
				EscortCallOrder::deleteData(['where'=>[$where]]);
				$text = '删除了叫号订单id='.$arr[$i].'的数据';
				$this->writeActionLog($text);
			}
		}
		$data['status']=1;
		return json($data);
	}
	//过号
    public function guohao(){
        $data = array('status' => 0,'msg' => '未知错误');
        $id=input("param.id");
        if(!$id){
            $data['msg']='参数错误';return json($data);exit;
        }

        //id开始叫号  修改状态为2
        $new=gettime();
        //Db::name('escort_call_order')->where('id',$id)->update(['status'=>3,'end_time'=>$new]);
		EscortCallOrder::updatesData(['where'=>['id'=>$id],'data'=>['status'=>3,'end_time'=>$new]]);
        $text = '修改了预约订单id='.$id.'的数据-已过号';

        //过号后发送消息
        //$res=Db::name('escort_call_order')->where('id',$id)->find();
		$res=EscortCallOrder::findData(['where'=>['id'=>$id]]);
        $event='jiaohao';//事件
        $orderid=$id;//订单id
		$bankid=$res['bankid'];
		//$title=Db::name('escort_call_calllist')->where('id',$bankid)->value('title');
		$title=EscortCallCalllist::valueData(['where'=>['id'=>$bankid],'field'=>'title']);
        $title=$title;//业务名称
        $name=$res['name'];//姓名
        $which=$res['which'];//号码
        $status=3;//状态 1预约成功 2正在叫号 3已过号 4开始办理 5取消预约 6预约完成
        $gui=$res['guitai'];//柜台
        $bankid=$res['bankid'];//银行id
        $isding=0;//是否置顶
        $datax=array('event'=>$event,'orderid'=>$orderid,'title'=>$title,'name'=>$name,'which'=>$which,'status'=>$status,'gui'=>$gui,'bankid'=>$bankid,'isding'=>$isding);
        WorkHandler::add($datax);


        $this->writeActionLog($text);
        $data['msg']='已过号';
        $data['status']=1;
        return json($data);
    }
	//叫号成功
    public function jiaohaogong(){
        $data = array('status' => 0,'msg' => '未知错误');
        $id=input("param.id");
        if(!$id){
            $data['msg']='参数错误';return json($data);exit;
        }
        //id开始叫号  修改状态为2
        $new=gettime();
        //Db::name('escort_call_order')->where('id',$id)->update(['status'=>6,'gong_time'=>$new]);
		$arr=['status'=>6,'gong_time'=>$new];
		EscortCallOrder::updatesData(['where'=>['id'=>$id],'data'=>$arr]);
        $text = '修改了预约订单id='.$id.'的数据-办理完成';
        $this->writeActionLog($text);
        $data['msg']='办理完成操作成功';

        //$res=Db::name('escort_call_order')->where('id',$id)->find();
		$res=EscortCallOrder::findData(['where'=>['id'=>$id]]);
        $event='jiaohao';//事件
        $orderid=$id;//订单id
		$bankid=$res['bankid'];
		//$title=Db::name('escort_call_calllist')->where('id',$bankid)->value('title');
		$title=EscortCallCalllist::valueData(['where'=>['id'=>$bankid],'field'=>'title']);
        $title=$title;//业务名称
        $name=$res['name'];//姓名
        $which=$res['which'];//号码
        $status=6;//状态 1预约成功 2正在叫号 3已过号 4开始办理 5取消预约 6预约完成
        $gui=$res['guitai'];//柜台
        $bankid=$res['bankid'];//银行id
        $isding=0;//是否置顶
        $datax=array('event'=>$event,'orderid'=>$orderid,'title'=>$title,'name'=>$name,'which'=>$which,'status'=>$status,'gui'=>$gui,'bankid'=>$bankid,'isding'=>$isding);
        WorkHandler::add($datax);
        
        $data['status']=1;
        return json($data);
    }

	//叫号
    public function jiaohao(){
        //$res=$this->isspeech();
        $data = array('status' => 0,'msg' => '未知错误');
        $id=input("param.id");
        if(!$id){
            $data['msg']='参数错误';return json($data);exit;
        }
        //id开始叫号  修改状态为2
        $new=gettime();
        //Db::name('escort_call_order')->where('id',$id)->update(['status'=>2,'start_time'=>$new]);
		EscortCallOrder::updatesData(['where'=>['id'=>$id],'data'=>['status'=>2,'start_time'=>$new]]);
        $text = '修改了预约订单id='.$id.'的数据-正在叫号';
        $this->writeActionLog($text);
        $data['msg']='叫号成功';

		//$res=Db::name('escort_call_order')->where('id',$id)->find();
		$res=EscortCallOrder::findData(['where'=>['id'=>$id]]);
        $event='jiaohao';//事件
        $orderid=$id;//订单id
		$bankid=$res['bankid'];
		//$title=Db::name('escort_call_calllist')->where('id',$bankid)->value('title');
		$title=EscortCallCalllist::valueData(['where'=>['id'=>$bankid],'field'=>'title']);
        $title=$title;//业务名称
        $name=$res['name'];//姓名
        $which=$res['which'];//号码
        $status=2;//状态 1预约成功 2正在叫号 3已过号 4开始办理 5取消预约 6预约完成
        $gui=$res['guitai'];//柜台
        $bankid=$res['bankid'];//银行id
        $isding=0;//是否置顶
        $datax=array('event'=>$event,'orderid'=>$orderid,'title'=>$title,'name'=>$name,'which'=>$which,'status'=>$status,'gui'=>$gui,'bankid'=>$bankid,'isding'=>$isding);
        WorkHandler::add($datax);
        
        $data['status']=1;
        return json($data);
    }
}